
var THHIS_PAGE_TYPE = 'ct_media';   //  页面配置标识(具体通过管理后台查看)
let relate_id = 17;
var PAGES = {
	
	//  初始化准备
	initial:function(){
		layui.use(function(){
		    var $ = layui.$;
		    var layer = layui.layer;
		    var util = layui.util;
		    var form = layui.form;
			
			  // 事件
			  util.on('lay-on', {
				    'tips-photos-one': function(e){
						var url = e.attr('src')
						layer.photos({
							photos: {
							  "title": "大图查看",
							  "start": 0,
							  "data": [{"pid": 5,"src": url,}]
							},
							footer: false
						});
				    },
					
					'on_href':function(e){
						var type = e.attr('data-type');
						var id = e.attr('data-id');
						
						switch (type*1){
							case 1:
								window.location.href="../component/index.html?id="+id
								break;
							case 2:
								window.location.href="../../country/html/index.html?id="+id
								break;
							case 3:
								window.location.href="../../district/html/index.html?id="+id
								break;	
							case 4:
								window.location.href="../../manufacturing/html/index.html?id="+id
								break;	
							case 5:
								window.location.href="../../auxiliary/html/index.html?id="+id
								break;	
							case 6:
								window.location.href="../../ppt/html/index.html?id="+id
								break;				
						}
					},
				  
				  'test-page-custom': function(e){
						var  this_id   = e.attr('data-id');
						var  this_type = e.attr('data-type');
						
				        layer.open({
				          type: 1,
						  scrollbar:false,
				          area: '600px',
				          resize: false,
				          shadeClose: true,
				          title: [this_type == 0 ? '内容新增填写' : '内容纠错填写', 'font-size:16px;font-weight: 600'],
				          content: `
				            <div class="layui-form" lay-filter="filter-test-layer" style="margin: 20px 16px 16px 16px;">
				              <div class="demo-login-container">
							  
				                <div class="layui-form-item">
				                  <div class="layui-input-wrap">
				                    <textarea name="content" value="" style="min-height: 165px;" lay-verify="required" placeholder="请填写内容" lay-reqtext="请填写内容" autocomplete="off" class="layui-textarea" lay-affix="clear"></textarea>
				                  </div>
				                </div>
								
				                <div class="layui-form-item" style="margin-top:20px;display: flex;align-items: center;justify-content: flex-end;">
									<button style="width:100px" class="layui-btn" lay-submit lay-filter="demo-login">提交</button>
				                </div>
				               
				              </div>
				            </div>
				          `,
				          success: function(){
				            // 对弹层中的表单进行初始化渲染
				            form.render();
							
				            // 表单提交事件
				            form.on('submit(demo-login)', function(data){
				              var field = data.field; // 获取表单字段值
							  field.content_id = this_id;
							  field.type = this_type;
							  field.url  = window.location.href;
							  PAGES.setPageCorrect(field)
				              return false; // 阻止默认 form 跳转
				            });
				          }
				        });
				      }
				  
			  })
			
		});
		
		//  获取到缓存数据 拼接左侧菜单
		//  制造业产业、技术品牌库
		if(localStorage.getItem("jingjiang_storages_data")){
			var d = JSON.parse(localStorage.getItem("jingjiang_storages_data"))
			var hrml1 = template('script_storages',  d);
			$("#view_storages").empty().append(hrml1);
		}else{
			PAGES.getHomeStorages();
		}
		
		// 国家库
		if(localStorage.getItem("jingjiang_countries_data")){
			var d = JSON.parse(localStorage.getItem("jingjiang_countries_data"))
			var hrml3 = template('script_countries', d);
			$("#view_countries").empty().append(hrml3); 
		}else{
			PAGES.getHomeCountries()
		}
		
		// 省市库
		if(localStorage.getItem("jingjiang_provinces_data")){
			var d = JSON.parse(localStorage.getItem("jingjiang_provinces_data"))
			var hrml4 = template('script_provinces', d);
			$("#view_provinces").empty().append(hrml4); 
		}else{
			PAGES.getHomeProvinces()
		}
		
		
		PAGES.getHomeSubs(8);
		PAGES.getHomeSubs(9);
		PAGES.getHomeSubs(10);
		
		PAGES.getPageData();
		
	},
	
	
	
	//  获取 制造业专区品牌库
	 getHomeSubs:function(v){
		// 先获取一下本地缓存
		var jingjiang_subs_data = localStorage.getItem("jingjiang_subs_data"+v)
		if(jingjiang_subs_data){
			try{
				var d = JSON.parse(jingjiang_subs_data)
				var hrml2 = template('script_subs'+v, d);
				$("#view_subs"+v).empty().append(hrml2); 
				
			}catch(e){
				
			}
		} 
		var datas = {
		 	url  :'/api/brand_category/subs',
		 	http_type:'POST',  
		 	datas:{parent_id:v}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
		 		var hrml2 = template('script_subs'+v, ret.data);
		 		$("#view_subs"+v).empty().append(hrml2); 
				
				// 存储在本地 每次请求先获取 本地信息
				localStorage.setItem("jingjiang_subs_data"+v, JSON.stringify(ret.data));
		 	}
		});
	 },
	
	
	
	//  获取 制造业各省市品牌库
	getHomeProvinces(v){
		var datas = {
		 	url  :'/api/region/provinces', 
		 	http_type:'POST',
			datas:{
				country_id:v
			}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
		 		var hrml4 = template('script_provinces', ret);
		 		$("#view_provinces").empty().append(hrml4); 
				// 存储在本地 每次请求先获取 本地信息
				localStorage.setItem("jingjiang_provinces_data", JSON.stringify(ret));
		 	}
		});
	},
	
	
	//  获取 制造业各国家品牌库
	getHomeCountries:function(){
		var datas = {
		 	url  :'/api/region/countries', 
		 	http_type:'POST',  
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
		 		var hrml3 = template('script_countries', ret);
		 		$("#view_countries").empty().append(hrml3); 
				// 存储在本地 每次请求先获取 本地信息
				localStorage.setItem("jingjiang_countries_data", JSON.stringify(ret));
		 	}
		});
	},
	
	
	//  获取 制造业产业、技术品牌库
	getHomeStorages:function(){
		var datas = {
			url  :'/api/brand_category/storages',
			http_type:'POST',  
			datas:{}
		}
		wxx.HttpReqeust(datas,function(ret){
			if(ret.code == 1){
				var hrml1 = template('script_storages', ret.data);
				$("#view_storages").empty().append(hrml1);
				// 存储在本地
				localStorage.setItem("jingjiang_storages_data", JSON.stringify(ret.data));
			}
		});
	},
	
	
	
	// 提交 新增和纠错
	setPageCorrect(obj){
		var datas = {
		 	url  :'/api/page/correct', 
		 	http_type:'POST',
			loading:true,
			datas:obj
		}
		wxx.HttpReqeust(datas,function(ret){
			layer.alert(ret.msg,{
				title:'温馨提示',
			},function(){
				console.log(ret)
				if(ret.code == 1){
					layer.closeAll()
				}else{
					layer.closeLast(); 
				}
			});
		 	
		});
	},
	
	//  获取 页面数据
	getPageData(v){
		var datas = {
		 	url  :'/api/page', 
		 	http_type:'POST',
			datas:{
				keyset:THHIS_PAGE_TYPE,
                "relate_id": relate_id, //国家ID
			}
		}
		wxx.HttpReqeust(datas,function(ret){
		 	if(ret.code == 1){
		 		var hrml = template('script_page', ret.data);
		 		$("#view_page").append(hrml); 
				
				//  调用一下公共分类展开收起等方法
				wxx.init_types();
				
				$('#view_page img').each(function(){
					var url = $(this).attr('src');
					$(this).addClass('fancybox').attr('data-fancybox-group','gallery').attr('href',url).attr('title','点击查看大图').attr('lay-on','tips-photos-one')
				})
				
				if($('.pageViews').length){
					$('.pageViews #number').html(ret.data.views)
				}
				
		 	}else{
				console.log('======>>>')
				$('.productDescription').remove()
				$('.template_title_introduce').remove()
				 
				var hrml = template('script_page', ret.data);
		 		$("#view_page").append(hrml); 
			}
		});
	},
	
	
}

PAGES.initial();

$("#national_table").click((e)=>{
    // 设置当前模块国家的ID
    // localStorage.setItem("brand_category_id",e.target.dataset.id || '130')
    relate_id = e.target.dataset.id;
	$('.nav_com').html(appendHtml(e.target.innerText));
    PAGES.getPageData();
    console.log('当前点击的对象id===>>>',e.target.dataset.id);
    console.log('当前点击的对象innerText===>>>',e.target.innerText);
})